package com.dl7.mvp.module.video.list;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;

import com.dl7.downloaderlib.entity.FileInfo;
import com.dl7.mvp.R;
import com.dl7.mvp.adapter.BaseVideoListAdapter;
import com.dl7.mvp.injector.components.DaggerAlbumsComponent;
import com.dl7.mvp.injector.modules.AlbumsModule;
import com.dl7.mvp.local.table.VideoInfo;
import com.dl7.mvp.module.base.BaseFragment;
import com.dl7.mvp.module.base.ILoadDataView;
import com.dl7.mvp.module.base.IRxBusPresenter;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.dl7.recycler.listener.OnRequestDataListener;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import rx.functions.Action1;

public class AlbumsFragment extends BaseFragment<IRxBusPresenter> implements ILoadDataView<List<VideoInfo>> {

    private static final String ALBUMS_ID = "albums_id";

    @BindView(R.id.rv_recommend_video_list)
    RecyclerView mRvVideoList;

    @Inject
    BaseVideoListAdapter mAdapter;
    private int mID;

    public static AlbumsFragment newInstance(int id) {
        AlbumsFragment fragment = new AlbumsFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ALBUMS_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mID = getArguments().getInt(ALBUMS_ID);
        }
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_video_list;
    }

    @Override
    protected void initInjector() {
        DaggerAlbumsComponent.builder()
                .applicationComponent(getAppComponent())
                .albumsModule(new AlbumsModule(this, mID))
                .build()
                .inject(this);
    }

    @Override
    protected void initViews() {
        RecyclerViewHelper.initRecyclerViewV(mContext, mRvVideoList, mAdapter);
        mAdapter.setRequestDataListener(new OnRequestDataListener() {
            @Override
            public void onLoadMore() {
                mPresenter.getMoreData();
            }
        });

        mPresenter.registerRxBus(FileInfo.class, new Action1<FileInfo>() {
            @Override
            public void call(FileInfo fileInfo) {
                mAdapter.updateDownload(fileInfo);
            }
        });
    }

    @Override
    protected void updateViews() {
        mPresenter.getData();
    }


    @Override
    public void loadData(List<VideoInfo> videoList) {
        mAdapter.updateItems(videoList);
    }

    @Override
    public void loadMoreData(List<VideoInfo> photoList) {
        mAdapter.loadComplete();
        mAdapter.addItems(photoList);
    }

    @Override
    public void loadNoData() {
        mAdapter.loadAbnormal();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.unregisterRxBus();
    }
}
